﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Authors : System.Web.UI.Page
{
    FileUpload _fileUpload;
    TextBox _txtUpload;
    Image _imgUpload;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[EClass.ESession.Admin.ToString()] == null)
        {
            Response.Redirect("Login.aspx", true);
        }
    }
    protected void _grdView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "New")
        {
            _grdView.DataSourceID = "";
            _grdView.DataBind();
        }
        if (e.CommandName == "Deleted")
        {
            //            DeleteCommand="DELETE FROM [Authors] WHERE [AuthorID] = @original_AuthorID AND [AuthorName] = @original_AuthorName AND (([Image] = @original_Image) OR ([Image] IS NULL AND @original_Image IS NULL)) AND (([Description] = @original_Description) OR ([Description] IS NULL AND @original_Description IS NULL))" 
            _SDSAuthors.ConflictDetection = ConflictOptions.OverwriteChanges;
            _SDSAuthors.Delete();
            _SDSAuthors.DataBind();

            _SDSAuthors.ConflictDetection = ConflictOptions.CompareAllValues;
            Title = "Deleted";
        }
        if (e.CommandName.Equals("Upload"))
        {
            bool up = false;
            for (int i = 0; (i < _grdView.Rows.Count && !up); i++)
            {
                GridViewRow row = _grdView.Rows[i];
                if (row.RowType == DataControlRowType.DataRow)
                {
                    try
                    {
                        _fileUpload = row.FindControl("_fileUpload") as FileUpload;
                        _txtUpload = row.FindControl("_txtUpload") as TextBox;
                        _imgUpload = row.FindControl("_imgUpload") as Image;
                        string strAuthorName = row.Cells[1].Text;

                        if (_fileUpload.HasFile)
                        {
                            _fileUpload.SaveAs(Server.MapPath("~/Images/Author_") + strAuthorName + ".jpg");
                            _txtUpload.Text = "~/Images/Author_" + strAuthorName + ".jpg";
                            _imgUpload.ImageUrl = _txtUpload.Text;
                            up = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script> alert '" + ex.ToString() + "' </script>");
                    }
                }
            }
        }

    }
    protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            _grdView.DataSourceID = _SDSAuthors.ID;
            _grdView.DataBind();
            Response.Write("<script> alert 'Insert' </script>");
        }
        if (e.CommandName.Equals("NewUpload"))
        {
            bool up = false;
            try
            {
                DetailsView view = sender as DetailsView;

                for (int i = 0; (i < view.Rows.Count && !up); i++)
                {
                    DetailsViewRow row = view.Rows[i];
                    if (row.RowType == DataControlRowType.DataRow)
                    {

                        _fileUpload = row.FindControl("_fileNewUpload") as FileUpload;
                        _imgUpload = row.FindControl("_imgNew") as Image;
                        string strAuthorName = row.Cells[0].Text;

                        if (_fileUpload.HasFile)
                        {
                            _fileUpload.SaveAs(Server.MapPath("~/Images/Author_") + strAuthorName + ".jpg");
                            _imgUpload.ImageUrl = "~/Images/Author_" + strAuthorName + ".jpg";
                            up = true;
                        }
                    }
                }
            }
            catch
            (Exception ex)
            {
                Response.Write("<script> alert '" + ex.ToString() + "' </script>");
            }
        }
    }
}